{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Version 1, failed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "input_line_7:14:9: error: unknown type name 'TreeNode'\n",
      "        TreeNode * trimBST(TreeNode * root, int low, int high) {\n",
      "        ^\n",
      "input_line_7:14:28: error: unknown type name 'TreeNode'\n",
      "        TreeNode * trimBST(TreeNode * root, int low, int high) {\n",
      "                           ^\n",
      "input_line_7:20:13: error: unknown type name 'TreeNode'\n",
      "    bool ok(TreeNode * node) {\n",
      "            ^\n",
      "input_line_7:28:17: error: unknown type name 'TreeNode'\n",
      "    void travel(TreeNode * root, TreeNode * parent_node, int direction) {\n",
      "                ^\n",
      "input_line_7:28:34: error: unknown type name 'TreeNode'\n",
      "    void travel(TreeNode * root, TreeNode * parent_node, int direction) {\n",
      "                                 ^\n",
      "input_line_7:84:5: error: unknown type name 'TreeNode'\n",
      "    TreeNode * correct_root = nullptr;\n",
      "    ^\n"
     ]
    },
    {
     "ename": "Interpreter Error",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "Interpreter Error: "
     ]
    }
   ],
   "source": [
    "/**\n",
    " * Definition for a binary tree node.\n",
    " * struct TreeNode {\n",
    " *     int val;\n",
    " *     TreeNode *left;\n",
    " *     TreeNode *right;\n",
    " *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n",
    " *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n",
    " *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n",
    " * };\n",
    " */\n",
    "class Solution {\n",
    "    public:\n",
    "        TreeNode * trimBST(TreeNode * root, int low, int high) {\n",
    "            this -> low = low;\n",
    "            this -> high = high;\n",
    "            this -> travel(root, nullptr, 1);\n",
    "            return this -> correct_root;\n",
    "        }\n",
    "\n",
    "    bool ok(TreeNode * node) {\n",
    "        if (node != nullptr) {\n",
    "            if ((this -> low <= node -> val) && (node -> val <= this -> high)) {\n",
    "                return true;\n",
    "            }\n",
    "        }\n",
    "        return false;\n",
    "    }\n",
    "\n",
    "    void travel(TreeNode * root, TreeNode * parent_node, int direction) {\n",
    "        if (root != nullptr) {\n",
    "            if (this -> correct_root == nullptr) {\n",
    "                if (this -> ok(root)) {\n",
    "                    this -> correct_root = root;\n",
    "                }\n",
    "            }\n",
    "\n",
    "            if (this -> ok(root -> left)) {\n",
    "                printf(\"left: %d\\n\", root -> val);\n",
    "                travel(root -> left, root, -1);\n",
    "            } else {\n",
    "                if (root -> left != nullptr) {\n",
    "                    if (!this -> ok(root -> left -> left) && !this -> ok(root -> left -> right)) {\n",
    "                        root -> left = nullptr;\n",
    "                    } else if (!this -> ok(root -> left -> left) && this -> ok(root -> left -> right)) {\n",
    "                        root -> left = root -> left -> right;\n",
    "                    } else if (this -> ok(root -> left -> left) && !this -> ok(root -> left -> right)) {\n",
    "                        root -> left = root -> left -> left;\n",
    "                    }\n",
    "                    travel(root -> left, root, -1);\n",
    "                }\n",
    "            }\n",
    "\n",
    "            if (this -> ok(root -> right)) {\n",
    "                printf(\"right: %d\\n\", root -> val);\n",
    "                travel(root -> right, root, 1);\n",
    "            } else {\n",
    "                if (root -> right != nullptr) {\n",
    "                    if (!this -> ok(root -> right -> left) && !this -> ok(root -> right -> right)) {\n",
    "                        root -> right = nullptr;\n",
    "                    } else if (!this -> ok(root -> right -> left) && this -> ok(root -> right -> right)) {\n",
    "                        root -> right = root -> right -> right;\n",
    "                    } else if (this -> ok(root -> right -> left) && !this -> ok(root -> right -> right)) {\n",
    "                        root -> right = root -> right -> left;\n",
    "                    }\n",
    "                    travel(root -> right, root, 1);\n",
    "                }\n",
    "            }\n",
    "        } else {\n",
    "            /*\n",
    "            if (parent_node != nullptr) {\n",
    "                printf(\"replaced: %d \\n\", root -> val);\n",
    "                if (this -> ok(root -> left) && !this -> ok(root -> right)) {\n",
    "                    parent_node = root -> left;\n",
    "                } else if ((!this -> ok(root -> left)) && (this -> ok(root -> right))) {\n",
    "                    parent_node = root -> right;\n",
    "                } else if ((!this -> ok(root -> left)) && (this -> ok(root -> right))) {\n",
    "                    parent_node -> left = nullptr;\n",
    "                    parent_node -> right = nullptr;\n",
    "                }\n",
    "            }\n",
    "            */\n",
    "        }\n",
    "    }\n",
    "    private:\n",
    "        int low;\n",
    "    int high;\n",
    "    TreeNode * correct_root = nullptr;\n",
    "};"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Version N, success"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "/**\n",
    " * Definition for a binary tree node.\n",
    " * struct TreeNode {\n",
    " *     int val;\n",
    " *     TreeNode *left;\n",
    " *     TreeNode *right;\n",
    " *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n",
    " *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n",
    " *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n",
    " * };\n",
    " */\n",
    "class Solution {\n",
    "    public:\n",
    "        TreeNode * trimBST(TreeNode * root, int low, int high) {\n",
    "            this -> low = low;\n",
    "            this -> high = high;\n",
    "\n",
    "            find_real_root(root);\n",
    "            travel(real_root, nullptr);\n",
    "\n",
    "            return real_root;\n",
    "        }\n",
    "\n",
    "    bool ok(TreeNode * root) {\n",
    "        if (root != nullptr) {\n",
    "            if ((low <= root -> val) && (root -> val <= high)) {\n",
    "                return true;\n",
    "            }\n",
    "        }\n",
    "        return false;\n",
    "    }\n",
    "    \n",
    "    void find_real_root(TreeNode* root) {\n",
    "        if (root == nullptr) {\n",
    "            return;\n",
    "        } else {\n",
    "            if (ok(root)) {\n",
    "                if (real_root == nullptr) {\n",
    "                    real_root = root;\n",
    "                    printf(\"found real root %d\\n\", real_root->val);\n",
    "                }\n",
    "            }\n",
    "\n",
    "            if (real_root == nullptr) {\n",
    "                find_real_root(root -> left);\n",
    "                find_real_root(root -> right);\n",
    "            } else {\n",
    "                return;\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "\n",
    "    void travel(TreeNode * root, TreeNode * parent) {\n",
    "        if (root == nullptr) {\n",
    "            return;\n",
    "        } else {\n",
    "            printf(\"val: %d\\n\", root -> val);\n",
    "\n",
    "            if (parent != nullptr) {\n",
    "                if ((root -> val < low)) {\n",
    "                    printf(\"left\\n\");\n",
    "                    root -> left = nullptr;\n",
    "                    parent -> left = root -> right;\n",
    "                    root = parent;\n",
    "                    travel(root -> left, root);\n",
    "                    return;\n",
    "                } else if ((root -> val > high)) {\n",
    "                    printf(\"right\\n\");\n",
    "                    root -> right = nullptr;\n",
    "                    parent -> right = root -> left;\n",
    "                    root = parent;\n",
    "                    travel(root -> right, root);\n",
    "                    return;\n",
    "                }\n",
    "            }\n",
    "            travel(root -> left, root);\n",
    "            travel(root -> right, root);\n",
    "\n",
    "            printf(\"left and right\\n\");\n",
    "        }\n",
    "    }\n",
    "\n",
    "    private:\n",
    "        int low;\n",
    "        int high;\n",
    "        TreeNode * real_root = nullptr;\n",
    "};"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## After two day 12 hours and one night 1 hour, finally I solved it.\n",
    "The hardest part is that you should know a `binary search tree` is a special data structure. It has a feature that says the left node will always smaller than the right node.\n",
    "\n",
    "I was trying to solve it as a normal binary tree at first but failed due to the complexity."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/trim-a-binary-search-tree\n",
    "\n",
    "\n",
    "Runtime: 20 ms, faster than 85.12% of C++ online submissions for Trim a Binary Search Tree.\n",
    "\n",
    "Memory Usage: 24.5 MB, less than 8.39% of C++ online submissions for Trim a Binary Search Tree.\n",
    "\n",
    "\n",
    "```cpp\n",
    "/**\n",
    " * Definition for a binary tree node.\n",
    " * struct TreeNode {\n",
    " *     int val;\n",
    " *     TreeNode *left;\n",
    " *     TreeNode *right;\n",
    " *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n",
    " *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n",
    " *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n",
    " * };\n",
    " */\n",
    "class Solution {\n",
    "    public:\n",
    "        TreeNode * trimBST(TreeNode * root, int low, int high) {\n",
    "            this -> low = low;\n",
    "            this -> high = high;\n",
    "\n",
    "            find_real_root(root);\n",
    "            travel(real_root, nullptr);\n",
    "\n",
    "            return real_root;\n",
    "        }\n",
    "\n",
    "    bool ok(TreeNode * root) {\n",
    "        if (root != nullptr) {\n",
    "            if ((low <= root -> val) && (root -> val <= high)) {\n",
    "                return true;\n",
    "            }\n",
    "        }\n",
    "        return false;\n",
    "    }\n",
    "    \n",
    "    void find_real_root(TreeNode* root) {\n",
    "        if (root == nullptr) {\n",
    "            return;\n",
    "        } else {\n",
    "            if (ok(root)) {\n",
    "                if (real_root == nullptr) {\n",
    "                    real_root = root;\n",
    "                    printf(\"found real root %d\\n\", real_root->val);\n",
    "                }\n",
    "            }\n",
    "\n",
    "            if (real_root == nullptr) {\n",
    "                find_real_root(root -> left);\n",
    "                find_real_root(root -> right);\n",
    "            } else {\n",
    "                return;\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "\n",
    "    void travel(TreeNode * root, TreeNode * parent) {\n",
    "        if (root == nullptr) {\n",
    "            return;\n",
    "        } else {\n",
    "            printf(\"val: %d\\n\", root -> val);\n",
    "\n",
    "            if (parent != nullptr) {\n",
    "                if ((root -> val < low)) {\n",
    "                    printf(\"left\\n\");\n",
    "                    root -> left = nullptr;\n",
    "                    parent -> left = root -> right;\n",
    "                    root = parent;\n",
    "                    travel(root -> left, root);\n",
    "                    return;\n",
    "                } else if ((root -> val > high)) {\n",
    "                    printf(\"right\\n\");\n",
    "                    root -> right = nullptr;\n",
    "                    parent -> right = root -> left;\n",
    "                    root = parent;\n",
    "                    travel(root -> right, root);\n",
    "                    return;\n",
    "                }\n",
    "            }\n",
    "            travel(root -> left, root);\n",
    "            travel(root -> right, root);\n",
    "\n",
    "            printf(\"left and right\\n\");\n",
    "        }\n",
    "    }\n",
    "\n",
    "    private:\n",
    "        int low;\n",
    "        int high;\n",
    "        TreeNode * real_root = nullptr;\n",
    "};\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
